package com.miracle.java2110.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.RoomManagerMapper;
import com.miracle.java2110.pojo.RoomManager;
import com.miracle.java2110.service.RoomService;
import com.miracle.java2110.vo.BarBo;
import com.miracle.java2110.vo.BarPo;
import com.miracle.java2110.vo.BarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("roomService")
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomManagerMapper roomManagerMapper;

    @Override
    public PageVo<RoomManager> pageData(Map<String, Object> map) {

        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
        List<RoomManager> roomManagers = roomManagerMapper.selectAll(map);

        PageInfo<RoomManager> pageInfo = new PageInfo<>(roomManagers);

        PageVo<RoomManager> pageVo = new PageVo<>();

        pageVo.setCode(0);
        pageVo.setMsg("查询房间分页数据");
        pageVo.setCount(pageInfo.getTotal());
        pageVo.setData(pageInfo.getList());

        return pageVo;
    }

    @Override
    public R add(RoomManager roomManager) {

        int i = roomManagerMapper.addSelective(roomManager);

        if (i != 0) {
            return R.ok(200,"添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @Override
    public R multiDelete(List<RoomManager> list) {

        int i = roomManagerMapper.multiDelete(list);

        if (i != 0) {
            return R.ok(200,"删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @Override
    public R edit(RoomManager roomManager) {

        int i = roomManagerMapper.updateByIdSelective(roomManager);

        if (i != 0) {
            return R.ok(200,"修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public R delete(Integer id) {
        int i = roomManagerMapper.deleteById(id);

        if (i != 0) {
            return R.ok(200,"修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public BarPo selectHistogram() {

        List<BarBo> barBos = roomManagerMapper.selectHistogram();

        BarPo barPo = new BarPo();

        ArrayList<String> roomTypeList = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();

        barPo.setPropertyList(roomTypeList);
        barPo.setNumList(numList);

        barBos.stream().forEach(s -> {
            String roomType = s.getRoomType();
            Integer num = s.getNum();

            barPo.getPropertyList().add(roomType);
            barPo.getNumList().add(num);

        });

        return barPo;
    }

    @Override
    public List<BarVo> selectPie() {
        return roomManagerMapper.selectPie();
    }
}
